<template>
    <div class="Search body">
        <div class="sh_top">
            <div class="sh_cont">
                <dl>
                    <dd>
                        <img src="../assets/img/i_icon_sousuo.png" alt="" />
                        <input
                            type="text"
                            :value="searchVal"
                            placeholder="大家都在搜"
                            @input="searchValInput"
                            @focus="inputFocus(true)"
                            @blur="inputFocus(false)"
                            @keydown.enter="enterSearch()"
                        />
                    </dd>
                    <dt @click="goSeash">
                        <span>洞健一下</span>
                    </dt>
                </dl>
                <div>
                    <span>大家都在搜：</span>
                    <i
                        v-for="(item, index) in hotList"
                        :key="index"
                        @click="allSearch(item.keyword)"
                        >{{ item.keyword }}</i
                    >
                </div>
            </div>
        </div>
        <div class="sh_box">
            <div class="lef">
                <div class="sh_history" v-if="false">
                    <dl class="sh_hi_title">
                        <dd><span>历史搜索</span></dd>
                        <dt>
                            <img
                                src="../assets/img/ss_icon_shanchu.png"
                                alt=""
                            />
                            <span>清除查询历史</span>
                        </dt>
                    </dl>

                    <ul>
                        <li
                            v-for="(item, index) in histtoryList"
                            :key="index"
                            @click="hisClick('上海疫情')"
                        >
                            上海疫情
                        </li>
                    </ul>
                </div>

                <div class="sh_list" v-if="type == 1 || type == 2">
                    <div class="sh_li_head">
                        <ul>
                            <li
                                :class="
                                    searchType == item.url ? 'active_sh' : ''
                                "
                                @click="typeChange(item)"
                                v-for="(item, index) in headerLi"
                                :key="index"
                            >
                                <span>{{ item.titel }}</span>
                            </li>
                        </ul>
                    </div>

                    <!-- <div class="sh_li_empty" v-if="type == 4">
                        关键字 "{{ searchVal }}" 搜索结果 0 条
                    </div> -->

                    <List
                        v-if="type == 1"
                        :searchVal="searchVal"
                        :searchClick="isClick"
                        :type="'search'"
                        :searchType="searchType"
                    />
                </div>
            </div>
            <div class="rig">
                <Popularity />
            </div>
        </div>
    </div>
</template>

<script>
import List from '../components/List';
import Popularity from '../components/Popularity';
import { mapState, mapMutations } from 'vuex';
import { axiosGet } from '../server/axios';
export default {
    name: 'SearCh',
    components: {
        List,
        Popularity,
    },

    data() {
        return {
            type: 0,
            icur: 0,
            hotList: [],
            histtoryList: [],
            isClick: 0,

            focusType: false,

            searchType: 'api/articles',
            headerLi: [
                {
                    titel: '文章',
                    url: 'api/articles',
                },
                // {
                //     titel: '短文本',
                //     url: 'api/posts',
                // },
                {
                    titel: '专题',
                    url: 'api/subjects',
                },
                // {
                //     titel: '用户',
                //     url: 'api/auth_users/dong_jian',
                // },
                {
                    titel: '活动',
                    url: 'api/activities',
                },
            ],
        };
    },
    computed: {
        ...mapState({
            searchVal: (state) => state.searchVal,
        }),
    },
    mounted() {
        this.getHotSearch();
        // this.getHistory();
        if (this.searchVal) {
            this.goSeash();
        }
    },
    methods: {
        ...mapMutations(['changeSsearchVal']),
        typeChange(item) {
            this.searchType = item.url;
        },
        enterSearch() {
            if (this.searchVal) {
                this.goSeash();
            }
        },
        inputFocus(type) {
            this.focusType = type;
        },
        searchValInput(e) {
            this.changeSsearchVal(e.target.value);
        },
        // 点击历史搜索
        hisClick() {
            // this.searchVal = text;
            this.type = 1;
        },
        allSearch(text) {
            this.changeSsearchVal(text);
            this.goSeash();
        },
        // 点击洞见一下
        goSeash() {
            this.type = 1;
            this.isClick = Math.random();
        },
        // 获取热搜
        getHotSearch() {
            axiosGet('api/search_histories/hot', {}, (res) => {
                console.log(res);
                if (res.code != 'ok') {
                    this.$notify({
                        title: '警告',
                        message: res.msg,
                        type: 'warning',
                    });
                }
                this.hotList = res.data;
            });
        },
        // 获取历史记录
        getHistory() {
            axiosGet('api/search_histories/my', {}, (res) => {
                console.log(res);
                if (res.code != 'ok') {
                    this.$notify({
                        title: '警告',
                        message: res.msg,
                        type: 'warning',
                    });
                }
                this.histtoryList = res.data;
            });
        },
    },
};
</script>
<style scoped lang="scss">
.sh_top {
    height: 160px;
    background: #fafafa;
    overflow: hidden;
}
.sh_cont {
    width: 872px;
    margin: 40px auto 0;

    dl {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 13px;

        dd {
            width: 750px;
            height: 51px;
            background: #ffffff;
            border-top: 1px solid #dddddd;
            border-left: 1px solid #dddddd;
            border-bottom: 1px solid #dddddd;
            border-radius: 4px 0 0 4px;
            display: flex;
            justify-content: flex-start;
            align-items: center;

            img {
                margin: 16px 0 16px 16px;
            }

            input {
                font-size: 16px;
                font-family: Source Han Sans SC VF;
                font-weight: 400;
                color: #000000;
                width: 680px;
                height: 40px;
                line-height: 40px;
                border: 0;
                outline: none;
                padding: 0 8px;
            }
        }

        dt {
            width: 120px;
            height: 50px;
            background: #e60012;
            border-radius: 0px 4px 4px 0px;
            border-top: 1px solid #e60012;
            border-right: 1px solid #e60012;
            border-bottom: 1px solid #e60012;
            font-size: 18px;
            font-family: Source Han Sans SC VF;
            font-weight: 400;
            color: #ffffff;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
        }
    }

    div {
        span,
        i {
            font-size: 14px;
            font-family: Source Han Sans SC VF;
            font-weight: 400;
            color: #787878;
        }

        i {
            margin-right: 10px;
            cursor: pointer;
        }
    }
}
.sh_box {
    width: 1200px;
    margin: 20px auto 50px;
    overflow: hidden;
}
.lef {
    float: left;
    width: 770px;
    overflow: hidden;
    box-shadow: 0 0 8px 4px #f5f5f5;
    margin-bottom: 8px;
}
.rig {
    width: 410px;
    float: right;
    overflow: hidden;
    box-shadow: 0 0 8px 4px #f5f5f5;
    margin-bottom: 8px;
}

.sh_history {
    overflow: hidden;
    .sh_hi_title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 46px;
        margin-bottom: 10px;

        dd {
            font-size: 18px;
            font-family: Source Han Sans SC VF;
            font-weight: 400;
            color: #262626;
            line-height: 55px;
        }

        dt {
            display: flex;
            justify-content: space-between;
            align-items: center;

            span {
                font-size: 16px;
                font-family: Source Han Sans SC VF;
                font-weight: 400;
                color: #a7a7a7;
                margin-left: 2px;
            }
        }
    }

    ul {
        li {
            background: #fafafa;
            padding: 10px 15px;
            margin: 0 20px 20px 0;
            float: left;
            font-size: 14px;
            font-family: Source Han Sans SC VF;
            font-weight: 400;
            color: #787878;
            cursor: pointer;
        }
    }
}

.sh_li_head {
    height: 60px;
    border-bottom: 2px solid #f6f6f6;
    position: relative;

    ul {
        height: 60px;
        width: 100%;
        position: absolute;
        left: 0;
        padding-left: 20px;
        bottom: 0;
        li {
            float: left;
            font-size: 18px;
            font-family: Source Han Sans SC VF;
            font-weight: 400;
            color: #262626;
            line-height: 60px;
            border-bottom: 2px solid transparent;
            padding: 0 6px;
            margin-right: 50px;
            text-align: center;
            cursor: pointer;
        }
        .active_sh {
            border-bottom: 2px solid #e60012;
            color: #e60012;
        }
    }
}

.sh_li_empty {
    height: 160px;
    font-size: 14px;
    font-family: Source Han Sans SC VF;
    font-weight: 400;
    color: #787878;
    line-height: 160px;
    text-align: center;
}
</style>
